import 'dart:async';

import 'package:flutter/material.dart';
import 'package:junyeyishu/utils/QGEvent.dart';
import 'package:video_player/video_player.dart';

class QGPlayVideo extends StatefulWidget {
  VideoPlayerController controller;

  QGPlayVideo({super.key, required this.controller});
  @override
  State<QGPlayVideo> createState() => _QGPlayVideoState();
}

class _QGPlayVideoState extends State<QGPlayVideo> {
  StreamSubscription? subs;
  @override
  dispose() {
    subs?.cancel();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    subs = eventBus.on<EventVideoInitialize>().listen((obj) {
      if (mounted) setState(() {});
    });
  }

  @override
  build(context) {
    return Scaffold(
      body: GestureDetector(
        onTap: () {
          Navigator.of(context).pop();
        },
        child: Container(
          color: Color.fromRGBO(0, 0, 0, 0.6),
          child: Center(
            child: widget.controller.value.isInitialized
                ? AspectRatio(
                    aspectRatio: widget.controller.value.aspectRatio,
                    child: VideoPlayer(widget.controller))
                : const SizedBox(
                    width: 50,
                    height: 50,
                    child: CircularProgressIndicator(color: Colors.white)),
          ),
        ),
      ),
    );
  }
}
